/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.SanPhamDTO;

/**
 *
 * @author tuan
 */
public class SanPhamDAO {
    
    private static final String MaPhieuSP = "MaSP000";
    private static final int lengthMa = 8;
    
    public SanPhamDAO() {
    }
    
    public String getNewMaSanPham() throws SQLException {
        String MaSP = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllSanPhamFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MASP").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaSP = MaSP.concat(MaPhieuSP + maxMa);
        return MaSP;
    }
    
    public ArrayList<SanPhamDTO> searchSanPhamByRule(SanPhamDTO sp) throws SQLException {
        ArrayList<SanPhamDTO> listSanPham = new ArrayList<>();
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetSanPhamByRuleFromDatabase(?, ?, ?, ?, ?, ?, ?", objs);
        SanPhamDTO sanpham;
        while (result.next()) {
            sanpham = new SanPhamDTO(result.getString("MASP"), result.getString("TENSP"), result.getLong("DONGIABAN"),
                    result.getLong("DONGIAMUA"), result.getInt("SOLUONGTON"), result.getString("KIEUSANPHAM"), result.getString("LOAISANPHAM"));
            listSanPham.add(sanpham);
        }
        return listSanPham;
    }
    
    public ArrayList<SanPhamDTO> GetSanPhamByMaSP(String MaSP) throws SQLException {
        ArrayList<SanPhamDTO> listSanPham = new ArrayList<>();
        Object[] objs = { MaSP };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetSanPhamByMaSPFromDatabase( ? )", objs);
        SanPhamDTO sanpham;
        while (result.next()) {
            sanpham = new SanPhamDTO(result.getString("MASP"), result.getString("TENSP"), result.getLong("DONGIABAN"),
                    result.getLong("DONGIAMUA"), result.getInt("SOLUONGTON"), result.getString("KIEUSANPHAM"), result.getString("LOAISANPHAM"));
            listSanPham.add(sanpham);
        }
        return listSanPham;
    }
    
    public int UpdateSanPham(SanPhamDTO sp) {
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateSanPhamToDatabase(?, ?, ?, ?, ?, ?, ?", objs);
    }
    
    public int DeleteSanPham(SanPhamDTO sp) {
        Object[] objs = { sp.getMaSP() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteSanPhamPNHToDatabase(?)", objs);
    }
    
    public int InsertSanPham(SanPhamDTO sp) {
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertSanPhamToDatabase(?, ?, ?, ?, ?, ?, ?", objs);
    }
}
